<?php

class Application_Model_StanyMapper
{

    protected $_dbTable;

    public function init()
    {
        $this->_dbTable = new Application_Model_DbTable_Stany();
    }

    public function setDbTable($dbTable)
    {
        $dbTable = new $dbTable();

        if (!$dbTable instanceof Zend_Db_Table_Abstract)
        {
            throw new Exception('Invalid table data gateway provided');
        }

        $this->_dbTable = $dbTable;
        return $this;
    }

    public function getDbTable()
    {
        if (null == $this->_dbTable)
        {
            $this->setDbTable('Application_Model_DbTable_Stany');
        }
        return $this->_dbTable;
    }

    public function podajStany()
    {
        $resultSet = $this->getDbTable()->fetchAll();

        $stany = array();

        foreach ($resultSet as $row)
        {
            $stan = new Application_Model_Stany();

            $stan->setIdStan($row->id_stan)
                    ->setNazwa($row->nazwa);

            $stany[] = $stan;
        }
        return $stany;
    }

    public function podajStan($id, Application_Model_Stany $stan)
    {
        $resultSet = $this->getDbTable()->find($id);
        $row = $resultSet->current();

        $stan->setSetIdStan($row['id_stan']);
        $stan->setNazwa($row['nazwa']);
    }

    public function listaKierunkow()
    {
        $resultSet = $this->getDbTable()->fetchAll();

        $stany = array();

        foreach ($resultSet as $row)
        {
            $stan = new Application_Model_Stany();

            $stan->setNazwa($row->miasto)
            ;

            $stany[] = $stan;
        }
        return $stany;
    }

}

